/*
 * StatisticalCodes.java 2008-10-15
 * This program was write for find out some kinds of 
 * file that we specify.
 */
package charles.testproject.statisticalcodes;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import charles.testproject.test.Test;

/**
 * @author Sancho_lai
 */
public class StatisticalCodes implements Test {
	
	//codes line count
	private long codeLines  = 0;
	//comment line count
	private long commentLines = 0;
	//white line count
	private long whiteLines   = 0;
	//number of java files was found
	private long statisticalJavaFiles = 0; 
	
	//input a directory that you want,but it will cost a NullPointException when you input a <root directory>
	private String path = "D:\\REFERENCE\\Java例子程序聊天坦克等\\j2seExample\\CH11\\源代码";
	
	public void test() {
		File file = new File(path);
		
		findSpecifiedDoc(file);
		
		System.out.println("StatisticalJavaFiles:" + statisticalJavaFiles);
		System.out.println("NormalLine:" + codeLines);
		System.out.println("ComentLine:" + commentLines);
		System.out.println("WhiteLine :" + whiteLines);
		
	}
	/**
	 * find all the files that fit for the Regular Expression you specify
	 * and then deal with it and count the lines and delete the class files
	 * you can extend it you want to be
	 */
	private void findSpecifiedDoc(File file) {
		File[] fileArray = file.listFiles();
		for(File child:fileArray) {
			if(child.isDirectory()) { //when child was a directory recursively call <findSpecifiedDoc> until get java file 
				findSpecifiedDoc(child);
			}else if(child.getName().matches(".*\\.java$")) { //when the <child> was a java file ,count the lines
				parser(child);
				statisticalJavaFiles++;
			}else if(child.getName().matches(".*\\.class$")){ //when the <child> was a class file , delete it
				deleteClassFile(child);
			}
		} 
	}
    /**
     * delete the class type file
     * @param child
     */
	private void deleteClassFile(File child) {
		child.delete();
	}
	/**
	 * parser the java file count the codeLines ...
	 * using the regular expression for judging
	 * @param child
	 */
	private void parser(File child) {
		BufferedReader br = null;
		boolean comment = false;
		String line = "";
		try {
			br = new BufferedReader(new FileReader(child));
			while((line = br.readLine()) != null) {
				line = line.trim();
				if(line.matches("^[\\s&&[^\\n]]*$")) { //
					whiteLines ++;
				} else if (line.startsWith("/*") && !line.endsWith("*/")) {//comment lines start with /* 
					commentLines ++;
					comment = true;	
				} else if (line.startsWith("/*") && line.endsWith("*/")) {
					commentLines ++;
				} else if (true == comment) {
					commentLines ++;
					if(line.endsWith("*/")) {
						comment = false;
					}
				} else if (line.startsWith("//")) {
					commentLines ++;
				} else { //it suppose that the java files just contain three kinds of lines
					codeLines ++;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(br != null) {
			try {
				br.close();
				br = null;
			} catch (IOException e) {
				e.printStackTrace();
		 	}
		  }
	    }
	}	
}
